package com.husd.framework.thread;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.Scanner;
import java.util.concurrent.ThreadPoolExecutor;

// 一台电脑能创建多少线程呢？
public class ManyThread {

    public static void main(String[] args) throws InterruptedException {

        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(10);
        taskExecutor.setMaxPoolSize(10);
        taskExecutor.setQueueCapacity(30000);
        taskExecutor.setKeepAliveSeconds(100);
        taskExecutor.setThreadNamePrefix("test_");
        taskExecutor.setAllowCoreThreadTimeOut(false);
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
        //线程池初始化
        taskExecutor.initialize();


        for (int i = 0; i < 30000; i++) {
            taskExecutor.execute(new Thread(() -> {
                // System.out.println("thread id :" + Thread.currentThread().getId());
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }));
        }

        new Thread(()-> {
            while (true) {
                System.out.println("当前待执行任务： "  + taskExecutor.getThreadPoolExecutor().getQueue().size());
                try {
                    Thread.sleep(1000  * 1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }).start();

        System.out.println("thread is waiting .....");
        System.out.println("thread is waiting .....");
        System.out.println("thread is waiting .....");
        //System.out.println("t id :" + t.getId());
        //System.out.println("t2 id :" + t2.getId());
        System.out.println("thread is waiting .....");

        Scanner input = new Scanner(System.in);
        input.next();
    }
}
